package servlets;

import ejbs.MessageQueue;
import ejbs.MessageQueueLocal;
import entities.UserEntity;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.EmailChecker;
import utils.MD5Coder;

/**
 * 
 * @author andrei costache
 *
 */
public class UserRegisterController extends UserController {

	@Override
	public void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
            super.doRequest(request, response);
            RequestDispatcher view = null;
            String submitted = request.getParameter("submit");
            if (submitted != null){
                //Check if all fields are completed.
                String first_name = request.getParameter("first_name");
                String last_name = request.getParameter("last_name");
                String email = request.getParameter("email");
                String pass = request.getParameter("password");
                String confirm = request.getParameter("confirm_password");
                boolean ok = true;
                if (first_name == null || first_name.trim().equals("")){
                    msg.addStatusMessage("Please enter a First name");
                    ok = false;
                }
                if (last_name == null || last_name.trim().equals("")){
                    msg.addStatusMessage("Please enter a Last name");
                    ok = false;
                }
                if (email == null || email.trim().equals("")){
                    msg.addStatusMessage("Please enter an Email");
                    ok = false;
                } else if(!EmailChecker.isEmailValid(email)){
                    msg.addStatusMessage("The email address is invalid!");
                    ok = false;
                } else if (userDAO.checkEmailExists(email)){
                     msg.addStatusMessage("The email already exists!");
                     ok = false;
                }
                if (pass == null || pass.trim().equals("")){
                    msg.addStatusMessage("Please enter a Password");
                    ok = false;
                }
                //Check if the passwords match.
                if (!pass.equals(confirm)){
                    msg.addStatusMessage("The passwords does not match!");
                    ok = false;
                }
                if (ok){
                    UserEntity user_entity = new UserEntity();
                    user_entity.setFirstname(first_name);
                    user_entity.setLastname(last_name);
                    user_entity.setMail(email);
                    user_entity.setPassword(MD5Coder.getEncodedText(pass));
                    user_entity.setStatus(new Integer(1));
                    user_entity.setCreated(System.currentTimeMillis()/1000);
                    user_entity.setChanged(System.currentTimeMillis()/1000);
                    System.out.println(user_entity);
                    userDAO.create(user_entity);
                    msg.addStatusMessage("The user has been created.");
                }
                view = request.getRequestDispatcher("/jsp/UserRegister.jsp");
            } else {
                view = request.getRequestDispatcher("/jsp/UserRegister.jsp");
            }
            request.setAttribute("messages", msg);
            view.forward(request, response);
	}

}
